<?php

namespace App\Services;

use App\Enum\Constants;
use Slowlyo\OwlAdmin\Models\AdminSetting;
use Slowlyo\OwlAdmin\Services\AdminService;

class SettingService extends AdminService
{
	protected string $modelName = AdminSetting::class;

	public function getSetting()
	{
		$columns = Constants::PAGE_COLUMNS[$this->request->page] ?? [];
		$data = [];
		if($columns){
			foreach ($this->query()->whereIn('key', $columns)->pluck('values','key')->toArray() as $key => $value){
				$key = str_replace($this->request->page.'_', '', $key);
				$data[$key] = $value;
			}
		}
		return $data;
	}

	public function setSetting()
	{
        $page = $this->request->page;
		$columns = Constants::PAGE_COLUMNS[$page] ?? [];
		if($columns){
			foreach ($columns as $column) {
				$postColumn = str_replace($page.'_', '', $column);
                $values = $this->request->input($postColumn);
				$this->query()->updateOrCreate(
					['key' => $column],
					['values' => $values]
				);
                cache()->put($page.':'.$postColumn,$values);
			}
		}
		return [];
	}

}
